package com.crazy.web.service.outer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crazy.model.major.dto.ArticleDto;
import com.crazy.model.major.entity.TArticle;
import com.crazy.model.major.entity.TLabel;
import com.crazy.model.major.service.TArticleService;
import com.crazy.web.service.outer.ShowService;
import com.crazy.web.service.share.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/18 15:21
 */
@Service
public class ShowServiceImpl implements ShowService {

    @Autowired
    private TArticleService tArticleService;

    @Autowired
    private LabelService labelService;

    @Override
    public ArticleDto getCanShowArticleDetail(Long articleId) {
        ArticleDto articleDto = new ArticleDto();
        QueryWrapper<TArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TArticle.Columns.valid, 1);
        queryWrapper.eq(TArticle.Columns.isPublic, 1);
        queryWrapper.eq(TArticle.Columns.isRelease, 1);
        queryWrapper.eq(TArticle.Columns.id, articleId);
        List<TArticle> articles = tArticleService.list(queryWrapper);
        if(articles!=null && articles.size()>0){
            TArticle article = articles.get(0);
            articleDto.setId(article.getId());
            articleDto.setTitleName(article.getTitleName());
            articleDto.setType(article.getType());
            articleDto.setContent(article.getContent());
            articleDto.setCreateTime(article.getCreateTime());
            articleDto.setReadTimes(article.getReadTimes());
            articleDto.setUserId(article.getUserId());
            List<TLabel> labels = labelService.findByArticleId(article.getId());
            articleDto.setLabelNames(labels.stream().map(TLabel::getLabelName).collect(Collectors.joining(",")));
        }else{
            return null;
        }
        return articleDto;
    }
}
